﻿@page "/Apartment/List"

@using Bookify.Web.DTO.Apartments
@using Bookify.Web.Services.Apartments
@using Bookify.Web.Services.Bookings
@using Bookify.Web.Services.Share


<PageTitle>公寓列表</PageTitle>

<h3>公寓列表</h3>

<MRow Class="pb-3">    

    <MCol Cols="12" Class="d-flex py-0 pb-3">
        <MSpacer></MSpacer>
        <MButton Color="primary" OnClick="LoadListDataAsync">搜索</MButton>
    </MCol>

</MRow>



<MDataTable Headers="_headers"
Items="_apartments"
ItemsPerPage="5"
Class="elevation-1">
    <TopContent>
        <MDialog @bind-Value="_dialog"
        MaxWidth="500">
            <MCard>
                <MCardTitle>
                    <span class="text-h5">@FormTitle</span>
                </MCardTitle>

                <MCardText>
                    <MContainer>
                        <MRow>
                            <MCol Cols="12"
                            Sm="6"
                            Md="12">
                                <MTextField @bind-Value="_editedItem.Id"
                                Label="公寓编号"></MTextField>
                            </MCol>
                            <MCol Cols="12"
                            Sm="6"
                            Md="12">
                                <MTextField @bind-Value="_editedItem.Name"
                                Label="公寓名称"></MTextField>
                            </MCol>
                            <MCol Cols="12"
                            Sm="6"
                            Md="6">
                                <MTextField @bind-Value="_editedItem.Currency"
                                Label="货币"></MTextField>
                            </MCol>
                            <MCol Cols="12"
                            Sm="6"
                            Md="6">
                                <MTextField Value="_editedItem.FormattedPrice"  Label="价格"></MTextField>
                            </MCol> 

                            <MCol Cols="11"
                            Sm="8">
                                <MMenu @bind-Value="_datemenu"
                                CloseOnContentClick="false"
                                NudgeRight="40"
                                Transition="scale-transition"
                                OffsetY
                                MaxWidth="290"
                                MinWidth="290">
                                    <ActivatorContent>
                                        <MTextField Value="DateRangeText"
                                        Label="日期范围"
                                        PrependIcon="mdi-calendar"
                                        Readonly
                                        @attributes="context.Attrs"></MTextField>
                                    </ActivatorContent>
                                    <ChildContent>
                                        @if (_datemenu)
                                        {
                                            <MDatePicker @bind-Value="_dates" Range FullWidth></MDatePicker>
                                        }
                                    </ChildContent>
                                </MMenu>
                            </MCol>


                        </MRow>
                    </MContainer>
                </MCardText>

                <MCardActions>
                    <MSpacer></MSpacer>
                    <MButton Color="blue darken-1"
                    Text
                    OnClick="Close">
                        取消
                    </MButton>
                    <MButton Color="blue darken-1"
                    Text
                    OnClick="Save">
                        保存
                    </MButton>
                </MCardActions>
            </MCard>
        </MDialog>
    </TopContent>

    <ItemColContent>
        @if (context.Header.Value == "actions")
        {
            <MTooltip Bottom Text="预定房间">
                <ActivatorContent Context="tooltipContext">
                    <MIcon Small Class="mr-2"
                    OnClick="()=>EditItem(context.Item)">mdi-pencil</MIcon>
                </ActivatorContent>
            </MTooltip>
        } 
        else if (context.Header.Text == "邮编")
        {
            @context.Item.Address.ZipCode
        }
        else
        {
            @context.Value
        }
    </ItemColContent>    

</MDataTable>


@code {
    [Inject]
    public ApartmentCaller ApartmentCaller { get; set; } = default!;

    [Inject]
    public BookingCaller BookingCaller { get; set; } = default!;

    private List<ApartmentDto> _apartments = new();

    private List<DataTableHeader<ApartmentDto>> _headers = new List<DataTableHeader<ApartmentDto>>
        {
           new ()
           {
            Text= "ID",
            Align= DataTableHeaderAlign.Start,
            Sortable= false,
            Value= nameof(ApartmentDto.Id)
          },
          new (){ Text= "名称", Value= nameof(ApartmentDto.Name)},
          new (){ Text= "描述", Value= nameof(ApartmentDto.Description)},
          new (){ Text= "价格", Value= nameof(ApartmentDto.FormattedPrice)},
          new (){ Text= "货币", Value= nameof(ApartmentDto.Currency)},
          new (){ Text= "邮编", Value= nameof(ApartmentDto.Address.ZipCode)},
          new (){ Text= "国家", Value= nameof(ApartmentDto.Address.Country)},
          new (){ Text= "城市", Value= nameof(ApartmentDto.Address.City)},
          new (){ Text= "街道", Value= nameof(ApartmentDto.Address.Street)},
          new (){ Text= "Actions", Value= "actions",Sortable=false }
        };

    private List<DateOnly> _dates = new List<DateOnly>
        {
            new DateOnly(2025,9,10),
            new DateOnly(2025,9,20)
        };

    public string DateRangeText => string.Join(" ~ ", _dates.Select(date => date.ToString("yyyy-MM-dd")));

    private bool _dialog;   
    private bool _datemenu;

    private ApartmentDto _editedItem = new ApartmentDto
        {
            Id = Guid.Empty,
            Name = "",
            Currency = "",
            Price = 0

        };
    private int _editedIndex = -1;

    public string FormTitle
    {
        get
        {
            return _editedIndex == -1 ? "New Item" : "Edit Item";
        }
    }

    // protected override async Task OnInitializedAsync()
    // {
    //     await base.OnInitializedAsync();
    //     //await LoadListDataAsync();
    // }

    private async Task LoadListDataAsync()
    {
        var result = await ApartmentCaller.GetListAsync();
        _apartments = result;
    }



    public void Close()
    {
        _dialog = false;
        _editedItem = new ApartmentDto();
        _editedIndex = -1;
    }

    public async Task Save()
    {
        if (_editedIndex > -1)
        {
            var item = _apartments[_editedIndex];
            item.Id = _editedItem.Id;
            item.Name = _editedItem.Name;
            item.Currency = _editedItem.Currency;
            item.Price = _editedItem.Price;    
            var (startDate, endDate) = DateServcie.ParseDateRange(DateRangeText);
            await BookingCaller.ReserveBookingAsync(_editedItem.Id, startDate, endDate);
        }
        else
        {
            _apartments.Add(_editedItem);
        }
        Close();
    }

    public void EditItem(ApartmentDto item)
    {
        _editedIndex = _apartments.IndexOf(item);
        _editedItem = new ApartmentDto()
            {
                Id = item.Id,
                Name = item.Name,
                Currency = item.Currency,
                Price = item.Price
            };
        _dialog = true;
    }

}
